package com.my.test.swing;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


/**
 * 
 * @author Creat
 *
 */
public class DataHandler {
	
	private static List records;

	public static List getCities(){
		List l = new ArrayList();
		l.add("AUS");
		l.add("ORD");
		l.add("IAH");
		l.add("SFO");
		l.add("DTW");
		l.add("DFW");
		l.add("PHL");
		l.add("ORL");
		l.add("LAS");
		return l;
	}
	
	public static List searchRecord(String src, String desc){
		if(records == null)	
			initRecords();
		List list = new ArrayList();
		for (Iterator iterator = records.iterator(); iterator.hasNext();) {
			Flight f = (Flight) iterator.next();
			if (f.getSrc().equals(src) && f.getDesc().equals(desc)) {
				list.add(f);
			}
		}
		return list;
	}
	
	public static void updateRecord(Object obj, int ticNum) throws Exception{
		Flight fli = (Flight) obj;
		for (int i = 0; i < records.size(); i++) {
			Flight f = (Flight) records.get(i);
			if(f.equals(fli)){
				if(f.getRemainTic() < ticNum){
					throw new Exception("剩余票数不够");
				}
				f.setRemainTic(f.getRemainTic() - ticNum);
				records.set(i, f);
				return;
			}
		}
	}
	
	private static void initRecords(){
		records = new ArrayList();
		List l = getCities();
		for (int i=0; i<100; i++){
			Flight f = new Flight();
			f.setSrc(l.get((int)(Math.random()*l.size())).toString());
			f.setDesc(l.get((int)(Math.random()*l.size())).toString());
			f.setRemainTic((int)(Math.random()*50));
			f.setFlightNo(String.valueOf((int)(Math.random()*9999)));
			records.add(f);
		}
	}
	
	public static void main(String[] args) {
		List l = getCities();
		for (int i=0; i<100; i++){
			Flight f = new Flight();
			f.setSrc(l.get((int)(Math.random()*l.size())).toString());
			f.setDesc(l.get((int)(Math.random()*l.size())).toString());
			f.setRemainTic((int)(Math.random()*50));
			f.setFlightNo(String.valueOf((int)(Math.random()*9999)));
			System.out.println((Math.random()*10));
		}
	}
}
